package com.wmlive.hhvideo.widget;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.HorizontalScrollView;
import android.widget.TextView;

import com.wmlive.hhvideo.utils.DensityUtils;
import com.wmlive.hhvideo.utils.KLog;

import cn.wmlive.hhvideo.R;

/**
 * Created by vhawk on 2017/6/22.
 */

public class MarqueeView extends HorizontalScrollView implements View.OnClickListener {

    private static final String TAG = "MarqueeView";

    private TextView tvS1;
    private TextView tvS2;
    private TextView tvS3;
    private AnimatorSet animatorSet;

    private int SINGLE_ANIM_TIME_INTERVAL = 6000;

    public MarqueeView(Context context) {
        super(context);
        initView(context);
    }

    public MarqueeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MarqueeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = View.inflate(context, R.layout.view_marquee, this);
        tvS1 = (TextView) view.findViewById(R.id.tv_s1);
        tvS2 = (TextView) view.findViewById(R.id.tv_s2);
        tvS3 = (TextView) view.findViewById(R.id.tv_s3);

        setHorizontalScrollBarEnabled(false);
        setClickable(false);
    }
//
    private OnClickListener listener;
    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        listener = l;
        tvS1.setOnClickListener(this);
        tvS2.setOnClickListener(this);
        tvS3.setOnClickListener(this);

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return false;
    }

    public void setTextContent(String textContent) {
        if (!TextUtils.isEmpty(textContent)) {
            tvS1.setText(textContent);
            tvS2.setText(textContent);
            tvS3.setText(textContent);
            initAnim();
        }
    }

    public void start() {
        if (animatorSet != null && !animatorSet.isStarted()) {
            animatorSet.start();
            KLog.v(TAG,"animatorSet start");
        }else {
            cancel();
            initAnim();
            animatorSet.start();
        }
    }

    private void initAnim() {

        int viewWidth = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        tvS1.measure(viewWidth, height);

        float width = tvS1.getMeasuredWidth();

        SINGLE_ANIM_TIME_INTERVAL = (int) (width * 8);

        ObjectAnimator animatorT1S1 = ObjectAnimator.ofFloat(tvS1, "translationX", 0f, -width);
        animatorT1S1.setInterpolator(new LinearInterpolator());
        animatorT1S1.setDuration(SINGLE_ANIM_TIME_INTERVAL);

        ObjectAnimator animatorT1S2 = ObjectAnimator.ofFloat(tvS1, "translationX", 2 * width, -width);

        animatorT1S2.setDuration(3 * SINGLE_ANIM_TIME_INTERVAL);
        animatorT1S2.setStartDelay(SINGLE_ANIM_TIME_INTERVAL);
        animatorT1S2.setInterpolator(new LinearInterpolator());
        animatorT1S2.setRepeatCount(ValueAnimator.INFINITE);


        ObjectAnimator animatorT2S1 = ObjectAnimator.ofFloat(tvS2, "translationX", 0, -2 * width);

        animatorT2S1.setDuration(2 * SINGLE_ANIM_TIME_INTERVAL);
        animatorT2S1.setInterpolator(new LinearInterpolator());


        ObjectAnimator animatorT2S2 = ObjectAnimator.ofFloat(tvS2, "translationX", width, -2 * width);

        animatorT2S2.setDuration(3 * SINGLE_ANIM_TIME_INTERVAL);
        animatorT2S2.setStartDelay(2 * SINGLE_ANIM_TIME_INTERVAL);
        animatorT2S2.setInterpolator(new LinearInterpolator());
        animatorT2S2.setRepeatCount(ValueAnimator.INFINITE);

        ObjectAnimator animatorT3S1 = ObjectAnimator.ofFloat(tvS3, "translationX", 0, -3 * width);

        animatorT3S1.setDuration(3 * SINGLE_ANIM_TIME_INTERVAL);
        animatorT3S1.setInterpolator(new LinearInterpolator());
        animatorT3S1.setRepeatCount(ValueAnimator.INFINITE);


        animatorSet = new AnimatorSet();

        animatorSet.play(animatorT3S1)
                .with(animatorT2S2)
                .with(animatorT1S2)
                .with(animatorT2S1)
                .with(animatorT1S1);
    }


    public void pause() {
        if (animatorSet != null && animatorSet.isRunning()) {
            animatorSet.pause();
            KLog.v(TAG, "pause");
        }
    }

    public void resume() {
        if (animatorSet != null && animatorSet.isPaused()) {
            animatorSet.resume();
            KLog.v(TAG, "resume");
        }
    }

    public void cancel() {

        KLog.v(TAG, "cancel");
        if (animatorSet != null) {
            animatorSet.cancel();
            animatorSet = null;
        }
        if (tvS1 != null) {
            tvS1.clearAnimation();
        }
        if (tvS2 != null) {
            tvS2.clearAnimation();
        }
        if (tvS3 != null) {
            tvS3.clearAnimation();
        }
    }

    @Override
    public void onClick(View v) {
        listener.onClick(this);
    }
}
